package com.cmict.data.bpm.controller.admin.definition;

import com.cmict.data.framework.common.pojo.CommonResult;
import com.cmict.data.framework.common.pojo.PageResult;
import com.cmict.data.bpm.controller.admin.definition.vo.process.BpmProcessDefinitionListReqVO;
import com.cmict.data.bpm.controller.admin.definition.vo.process.BpmProcessDefinitionPageItemRespVO;
import com.cmict.data.bpm.controller.admin.definition.vo.process.BpmProcessDefinitionPageReqVO;
import com.cmict.data.bpm.controller.admin.definition.vo.process.BpmProcessDefinitionRespVO;
import com.cmict.data.bpm.service.definition.BpmProcessDefinitionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.cmict.data.framework.common.pojo.CommonResult.success;

@Api(tags = "管理后台 - 流程定义")
@RestController
@RequestMapping("/bpm/process-definition")
@Validated
public class BpmProcessDefinitionController {

    @Resource
    private BpmProcessDefinitionService bpmDefinitionService;

    @GetMapping("/page")
    @ApiOperation(value = "获得流程定义分页")
    @PreAuthorize("@ss.hasPermission('bpm:process-definition:query')")
    public CommonResult<PageResult<BpmProcessDefinitionPageItemRespVO>> getProcessDefinitionPage(
            BpmProcessDefinitionPageReqVO pageReqVO) {
        return success(bpmDefinitionService.getProcessDefinitionPage(pageReqVO));
    }

    @GetMapping ("/list")
    @ApiOperation(value = "获得流程定义列表")
    @PreAuthorize("@ss.hasPermission('bpm:process-definition:query')")
    public CommonResult<List<BpmProcessDefinitionRespVO>> getProcessDefinitionList(
            BpmProcessDefinitionListReqVO listReqVO) {
        return success(bpmDefinitionService.getProcessDefinitionList(listReqVO));
    }

    @GetMapping ("/get-bpmn-xml")
    @ApiOperation(value = "获得流程定义的 BPMN XML")
    @ApiImplicitParams({
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class),
            @ApiImplicitParam(name = "processInstanceId", value = "编号", required = true, example = "1024", dataTypeClass = String.class)}
    )
    @PreAuthorize("@ss.hasPermission('bpm:process-definition:query')")
    public CommonResult<Map> getProcessDefinitionBpmnXML(@RequestParam("id") String id , String processInstanceId ) {
        String processDefinitionBpmnXML = bpmDefinitionService.getProcessDefinitionBpmnXML(id);
        Map<String, Object> buttonMap = null;
        if(StringUtils.isNoneBlank(processInstanceId)){
            buttonMap = bpmDefinitionService.readxmlByDom(processDefinitionBpmnXML, processInstanceId);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("bpmnXml",processDefinitionBpmnXML);
        map.put("bpmnButton",buttonMap);
        return success(map);
    }
}
